//
//  XMSMainController.m
//  XMSimonUtils_Example
//
//  Created by 徐盟 on 2023/3/17.
//  Copyright © 2023 xumeng. All rights reserved.
//

#import "XMSMainController.h"
#import "XMSLoggerController.h"
#import "XMSBaseController.h"
#import "XMSHelperController.h"
#import "XMSCategoryController.h"

@interface XMSMainController ()
@property(nonatomic, strong) NSArray *navs;
@end

@implementation XMSMainController


- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (NSArray *)navs
{
    // 懒加载
    if (_navs == nil) {
        NSMutableArray * models = [NSMutableArray array];
        NSArray * dicts = [self getNavDicts];
        for (NSDictionary * dict in dicts) {
            XMSNav * nav = [XMSNav selfWithDict:dict];
            [models addObject:nav];
        }
        _navs = models;
    }
    return _navs;
}

-(NSArray<NSDictionary *> *) getNavDicts
{
    NSArray<NSDictionary *> * dicts = @[
        @{@"name":@"Logger", @"controller": NSStringFromClass(XMSLoggerController.class)},
        @{@"name":@"Helper", @"controller": NSStringFromClass(XMSHelperController.class)},
        @{@"name":@"分类", @"controller": NSStringFromClass(XMSCategoryController.class)},
    ];
    
    return  dicts;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return  self.navs.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    XMSNav * nav = self.navs[section];
    
    return nav.items.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    XMSNav * nav = self.navs[section];
    return  nav.name;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XMSNav * nav = self.navs[indexPath.section];
    XMSNavItem * item = nav.items[indexPath.row];
    static NSString * ID = @"nav_cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: ID];
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = item.name;
    if (item.subtitle) {
        cell.detailTextLabel.text = item.subtitle;
    }
    
    
    
    return  cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  52;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    XMSNav * nav = self.navs[indexPath.section];
    XMSNavItem * item = nav.items[indexPath.row];
    
    Class Clazz = NSClassFromString(nav.controller);
    
    XMSBaseController *controller = [[Clazz alloc] init];
    
    controller.navItem = item;
    controller.navigationItem.title = nav.name;
    [self.navigationController pushViewController:controller animated:YES];
}

@end
